<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * seo类
 * @package  module/base/content
 * @author   allen.qiang
 * @version 1.0
 */

class seo {

    /**
     * 查询SEO信息
     * @param array $arg_array
     * @return array|null
     */
    public function search( &$arg_array ) {
        
        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        if (isset($arg_array['uid']) && is_numeric($arg_array['uid']) && $arg_array['uid'] > 0 ) {
            $sql .= ' AND uid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uid'];
        }

        if (isset($arg_array['pageType']) && in_array($arg_array['pageType'] , array(1,2,3)) ) {
            $sql .= ' AND pageType = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pageType'];
        }

        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'] , array(0, 1)) ) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_seo ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_seo ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'  => $item->creator,
                    'createTime' => $item->createTime,
                    'pageName' => $item->pageName,
                    'uid' => $item->uid,
                    'pageType' => $item->pageType,
                    'title' => $item->title,
                    'keywords' => $item->keywords,
                    'description' => $item->description,
                    'useFlg' => $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;


        }

    }

    /**
     * 生成前台页面seo数据
     * @param string $prefix title前缀
     * @param int $pageType 页面代码（1.首页 2.列表页 3.详情页）[默认为空]
     * @param string $uid uid字段[默认为空]
     * @param array $replaces 需要替换的字符
     * @return array
     */
    public function shopSeo($prefix, $pageType = null, $uid = null, $replaces = null) {

        if (!empty($pageType) || !empty($uid)) {

            $arg_array = array(
                'uid' => $uid,
                'pageType' => $pageType,
                'useFlg' => 1
            );
            $temp_seo_array = $this->search( $arg_array );

        } else {

            $temp_seo_array = null;

        }

        if (!$temp_seo_array) {
            $temp_seo_array = array(
                'title' => null,
                'keywords' => null,
                'description' => null,
            );
        } else {
            $temp_seo_array = $temp_seo_array[0];
        }

        $seo_array = array(

            'title' => empty($temp_seo_array['title']) ? G_SEO_TITLE : $temp_seo_array['title'],
            'keywords' => empty($temp_seo_array['keywords']) ? G_SEO_KEYWORDS : $temp_seo_array['keywords'],
            'description' => empty($temp_seo_array['description']) ? G_SEO_DESCRIPTION : $temp_seo_array['description']

        );

        //add sunqiang 20141217 54-FUN-BK-0043-产品详情页TKD调整 start
        if($replaces && is_array($replaces))
        {
            foreach($replaces as $key=>$value)
            {
                $seo_array['title'] = $shop_array['seo']['title'] = str_replace($key, $value , $seo_array['title']);
                $seo_array['keywords'] = $shop_array['seo']['keywords'] = str_replace($key, $value , $seo_array['keywords']);
                $seo_array['description'] = $shop_array['seo']['description'] =str_replace($key, $value , $seo_array['description']);
            }

        }
        //add sunqiang 20141217 54-FUN-BK-0043-产品详情页TKD调整 start
        if (!empty($prefix))
            $seo_array['title'] = $prefix . ' - ' . $seo_array['title'];

        return $seo_array;

    }

}